//
//  SystemCDefinitions.cpp
//  sad
//
//  Created by Christopher Kemsley on 2/5/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#include <iostream>
#include "SystemCDefinitions.hpp"
#include "XMLParser.hpp"
#include "Model.hpp"



/**** URL XML Decoder ****/

struct	IntervalParser : public XMLParsingAgent
{
	IntervalParser ()
	:	XMLParsingAgent ("interval") { }
	
	
	virtual XMLParser::Offset	parse	( std::string const & file , Any & interval , XMLParser::Offset iStart )
		{
		std::string			stringValue ;
		XMLParser::Offset	length	=	XMLParserHelper :: getImmediateString ( file , iStart , stringValue ) ;
		
		
		if ( length )
			{
			std::stringstream	str		( stringValue ) ;
			
			double		value	=	0 ;
			std::string	unit ;
			
			str >> value ;
			str >> unit ;
			
			if ( unit.find("ns") != std::string::npos )
				interval = SCInterval ( value , kSCIntervalNanoSeconds ) ;
			
			else if ( unit.find("ms") != std::string::npos )
				interval = SCInterval ( value , kSCIntervalMiliSeconds ) ;
			
			else if ( unit.find("fs") != std::string::npos )
				interval = SCInterval ( value , kSCIntervalFemtoSeconds ) ;
			
			else if ( unit.find("us") != std::string::npos )
				interval = SCInterval ( value , kSCIntervalMicroSeconds ) ;
			
			else if ( unit.find("ps") != std::string::npos )
				interval = SCInterval ( value , kSCIntervalPicoSeconds ) ;
			
			else
				interval = SCInterval ( value , kSCIntervalSeconds ) ;
			}
		
		
		return iStart + length + /*"</interval>"*/11 ;
		}
} ;
IntervalParser _IntervalParser ;








/**** URL XML Decoder ****/

struct	VariantParser : public XMLParsingAgent
{
	VariantParser ()
	:	XMLParsingAgent ("variant") { }
	
	
	virtual XMLParser::Offset	parse	( std::string const & file , Any & value , XMLParser::Offset iStart )
		{
		static std::string const	_endTag	( "</variant>" ) ;
		XMLParser::Offset	end			=	file . find ( _endTag , iStart ) ;
		XMLParser::Offset	newOffset	=	end + _endTag.size() ;
		
		static std::string const	_configName		( "<configURL>" ) ;
		static std::string const	_instanceName	( "<instanceName>" ) ;
		static std::string const	_builderName	( "<builderName>" ) ;
		
		XMLParser::Offset	pConfigName			=	file . find ( _configName , iStart ) ;
		XMLParser::Offset	pInstanceName		=	file . find ( _instanceName , iStart ) ;
		XMLParser::Offset	pBuilderName		=	file . find ( _builderName , iStart ) ;
		
		if ( pConfigName>end || pInstanceName>end || pBuilderName>end )
			ThrowException ( "XMLParsingAgent::VariantParser" , "Failed to find the necessary arguments for the <variant> tag. {"<<pConfigName<<","<<pInstanceName<<","<<pBuilderName<<"}>"<<end<<"" , __FILE__,__LINE__ ) ;
		
		
		
		value	=	ModelVariant() ;
		ModelVariant & variant	=	value.as<ModelVariant>() ;
		
		
		std::string sConfigName ;
		XMLParserHelper :: getImmediateString ( file , pConfigName+_configName.size() , sConfigName ) ;
		
		
		variant.configURL = sConfigName ;
		XMLParserHelper :: getImmediateString ( file , pInstanceName+_instanceName.size() , variant.instanceName ) ;
		XMLParserHelper :: getImmediateString ( file , pBuilderName+_builderName.size() , variant.builderName ) ;
		
		
		return newOffset ;
		}
} ;
VariantParser _VariantParser ;



